<?php


namespace app\admin\controller;


use app\model\Goods;
use app\model\Member;
use app\model\MemberAuthGroup;
use app\model\MemberImage;
use think\exception\ValidateException;

class MemberController extends Base
{
    function search(){
        $uniacid = uniacid();
        $keyword = input('post.keyword', '', 'serach_in');
        $query = Member::where(['uniacid' => $uniacid]);

        if(!empty($keyword)) {
            $query->where('telephone|nickname', 'like', '%' . $keyword . '%');
        }


        $res = $query->field("id,telephone,nickname")->select()->toArray();

        $data['data'] = $res;
        return $this->json($data);
    }

    function auditList(){
        $uniacid = uniacid();
        $keyword = input('post.keyword', '', 'serach_in');

        $query = Member::where(['uniacid' => $uniacid,'is_vip'=>0,'is_vip_apply'=>1,'vip_apply_status'=>0]);

        if(!empty($keyword)) {
            $query->where('telephone|nickname', 'like', '%' . $keyword . '%');
        }

        $res = $query->order('sort asc,id desc')
            ->paginate(getpage())
            ->toArray();

        $data['data'] = $res;

        return $this->json($data);
    }

    function auditDetail(){
        $uid = input('post.id');
        if(empty($uid)){
            return $this->json([]);
        }

        $res = Member::field('id,nickname,xingming,is_vip,is_vip_apply,vip_apply_status,vip_apply_denial_reason,telephone,id_cart,id_cart_img_1,id_cart_img_2,disability_cert')->find($uid);
        if(!empty($res)){
            $res = $res->toArray();
        }

        $res['images'] = MemberImage::where(['mid'=>$uid])->select()->toArray();

        return $this->json(['data'=>$res]);
    }

    function audit(){
        $uid = input('post.id');
        $status = input('post.vip_apply_status');
        if($status === 0) {
            $status =2 ;
        }
        $data['id'] = $uid;

        $reason = '';
        if($status == 2) {
            $reason = input('post.vip_apply_denial_reason');
            if(empty($reason)){
                return $this->json(['errno'=>-1,'message'=>'拒绝原因不能为空']);
            }
        }else{
            $data['is_vip'] = 1;

            $membergroup = MemberAuthGroup::where(['uniacid' => uniacid(),'is_default'=>0])
                ->order('id asc')
                ->find();

            if(empty($membergroup)){
                return $this->json(['errno'=>-1,'message'=>'系统异常']);
            }

            $data['gid'] = $membergroup->id;
        }

        $data['is_vip_apply'] = 0;
        $data['vip_apply_status'] = $status;
        $data['vip_apply_denial_reason'] = $reason;

        Member::update($data);
        return $this->json(['errno'=>0]);
    }

    public function delete()	{
        $idx =  $this->request->post('id', '', 'serach_in');
        if (!$idx) throw new ValidateException('参数错误');
        Member::destroy(['id' => explode(',', $idx)], true);
        return $this->json(['msg' => '操作成功']);
    }
}
